﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Com.Toook.Common;
using System.Data.SQLite;

public partial class joke_content : BaseShowPage
{
    protected string JokeTitle;
    protected string JokeContent;
    protected string PreNext = "";//前一条、后一条

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitJoke();
        }
    }
    private void InitJoke()
    {
        int id = ParamHelper.ParseIntPositive(Request["id"]);

        DataRow row = PageData.SelectParamRow("typeCode='joke'");
        int total = 0;
        int.TryParse(row[PageColumn.totalCount].ToString(), out total);

        string sql;
        if (id > 0)
        {            
            sql = "select id, title, content from JokePublish where id>=" + (id - 1).ToString() + 
                " and id<=" + (id + 1).ToString() + " order by id desc ";
        }        
        else
        {
            sql = "select id, title, content from JokePublish order by id desc limit 0, 3";
        } 
        SQLiteConnection conn = SqliteHelper.GetConnection("SQLiteDatabase");
        SQLiteCommand cmd = new SQLiteCommand(sql, conn);
        SQLiteDataReader reader = null;

        try
        {
            bool first = true;
            int n = 1;
            conn.Open();
            reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                if (id > 0)
                {
                    if (reader["id"].ToString() != id.ToString())
                    {
                        if (first)
                        {
                            PreNext = "上一条：<a href=\"content.aspx?id=" + reader["id"].ToString() + "\">" + reader["title"].ToString() + "</a>";
                            first = false;
                        }
                        else
                        {
                            PreNext += "下一条：<a href=\"content.aspx?id=" + reader["id"].ToString() + "\">" + reader["title"].ToString() + "</a>";
                        }
                    }
                    else
                    {
                        JokeContent = reader["content"].ToString();
                        JokeTitle = reader["title"].ToString();
                    }
                }
                else
                {
                    if (n == 1)
                    {
                        PreNext = "上一条：<a href=\"content.aspx?id=" + reader["id"].ToString() + "\">" + reader["title"].ToString() + "</a>";
                        n++;
                    }
                    else if (n == 2)
                    {
                        JokeContent = reader["content"].ToString();
                        JokeTitle = reader["title"].ToString();
                        n++;
                    }
                    else
                    {
                        PreNext += "下一条：<a href=\"content.aspx?id=" + reader["id"].ToString() + "\">" + reader["title"].ToString() + "</a>";
                    }
                }
                
            }
        }
        finally
        {
            SqliteHelper.CloseDbResource(conn, reader);
        }
    }
}
